*-------------------------------------------------------------------------------------------------------*
*RESEARCHERS:		Mette Foged, Linea Hasager and Vasil Yasenov
*PROJECT:			Meta-analysis
*DESCRIPTION:		1  Regressions with full sample (incl. |effect sizes| >3
*-------------------------------------------------------------------------------------------------------*

clear all
set more off

global x_macro	 	Unemployment GDPgrowth
global x_ctr		eu restworld
global x_study 		i.method i.data_frequency i.skill
	

use "${data}\database_clean_full.dta", replace



*-------------------------------------------------------------------------------------------------------*
*-------------------------------------------------------------------------------------------------------*

drop region

* Rest of the world
gen restworld=0
replace restworld=1 if eu==0 & northamerica==0

*Nordic countries
gen nordic=0
replace nordic=1 if country == "Norway" | country == "Sweden" |country == "Denmark" |country == "Finland"
	
*Rest of Europe
gen resteu=0
replace resteu=1  if country=="Austria" | country=="France" | country=="Germany" | country=="Greece" ///
| country=="Ireland" | country=="Italy" | country=="Netherlands" | country=="Portugal" | country=="Spain" ///
| country=="Switzerland" | country=="United Kingdom" | country=="Western Europe" | country=="European Economic Area" | country=="Albania"



replace wage = 0 if wage == .
tab wage empl, m

replace skill="All" if skill=="all"
replace skill="High Skill" if skill=="High"
replace skill="Low Skill" if skill=="Low"

replace data_frequency="Short Run" 	if data_frequency=="Annual or more frequent"
replace data_frequency="Long Run" 	if data_frequency=="Less frequent"

gen yearfe = int(firstyear/10) * 10
gen pubyearfe = int(year/10)*10



gen top100="Not top 100"
replace top100="Top 100" if journal_ranking < 101

gen top50="Outside Top 50"
replace top50="Top 50" if journal_ranking < 51

gen avg=1 if variation=="Area"
replace avg=0 if variation=="Skill" | variation=="Mixture"

gen refugee="Refugees" if refugees==1
replace refugee="Workers, mixture or other" if refugees==0

summ ratio_f_n, d
gen relativelowskill="Relatively Low Skilled" if ratio_f_n !=.
replace relativelowskill="Relatively High Skilled" if ratio_f_n < 1 & ratio_f_n !=.
replace relativelowskill="Unknown" if ratio_f_n==.


* Weights
gen p_se=1/se
gen p_score=1/Score



* Distinguish relative versus average effects
tab variation
gen relative=0
replace relative=1 if variation=="Skill" | variation=="Mixture" 
replace relative=. if variation !="Skill" & variation!="Mixture" & variation !="Area"
gen area=0
replace area=1 if variation=="Area"



*generate numeric variables that can be used as factors
tab method
gen method_d=0
replace method_d=1 if inlist(method,"IV","DIDIV","FDIV")
drop method
rename method_d method


tab data_frequency
gen data_t=0
replace data_t=1 if data_frequency=="Short Run"
drop data_frequency
rename data_t data_frequency

tab skill
gen skill_t=0
replace skill_t=1 if skill=="All"
replace skill_t=2 if skill=="Low Skill"
replace skill_t=3 if skill=="High Skill"
drop skill
rename skill_t skill

tab relativelowskill
gen skill_t=0
replace skill_t=1 if relativelowskill=="Relatively Low Skilled"
replace skill_t=2 if relativelowskill=="Relatively High Skilled"
replace skill_t=3 if relativelowskill=="Unknown"
drop relativelowskill
rename skill_t relativelowskill


gen region_t = 0
replace region_t=1 if nordic==1
replace region_t=2 if resteu==1
replace region_t=3 if restworld==1
rename region_t region


label variable StrictEmpInd "\emph{Employment Rigidity}"
label variable CollBarg "\emph{Wage Rigidity}"
label variable AverageTenure "\emph{Labor Immobility}"
label variable ALMPExp "\emph{ALMP}"
label variable StateControl "\emph{Business Regulation}"


label var firstyear "Year"
label var nordic "Nordic countries"
label var resteu "Rest of Europe"
label var restworld "Rest of the world"
label var ratio_f_n "Low skilled immigrants/low skilled natives"
label define freq 0 "Long run" 1 "Short run"
label val data_frequency freq
label define method 0 "OLS" 1 "Simulation" 2 "IV"
label val method method
label define skill 1 "All skills" 2 "Low skill" 3 "High skill"
label val skill skill
label define region 0 "North America" 1 "Nordic countries" 2 "Rest of Europe" 3 "Rest of the world"
label val region region


tab data_frequency
gen data_t=0
replace data_t=1 if data_frequency==1
drop data_frequency
rename data_t data_frequency

tab skill
gen skill_t=0
replace skill_t=1 if skill==1
replace skill_t=2 if skill==2
replace skill_t=3 if skill==3
drop skill
rename skill_t skill



label variable StrictEmpInd_avg "\emph{EPL (Regular)}"
label variable StrictEmpColl_avg "\emph{EPL (Collective)}"
label variable StrictEmpTemp_avg "\emph{EPL (Temporary)}"
label variable CollBarg_avg "\emph{Collective Bargaining}"
label variable AverageTenure_avg "\emph{Average Job Tenure}"
label variable NetReplacementRate_avg "\emph{Net Replacement Rate}"
label variable PMR_avg "\emph{Product Market Regulation}"


*-------------------------------------------------------------------------------------------------------*
* TABLE : MAIN RESULTS
*-------------------------------------------------------------------------------------------------------*

gen cluster=paper_id

local wage_txt "Wage Effects"
local empl_txt "Employment Effects"

foreach type in wage empl {
preserve
keep if `type'==1


*Dummy for dropping OLS if there are IV estimates within study
gen iv=0
replace iv=1 if method==2
gen ols=0
replace ols=1 if method==0
bysort paper_id: egen _iv=sum(iv)

gen _keep=1
replace _keep=0 if ols==1 &  _iv>0
drop _iv ols iv



local inst StrictEmpInd_avg
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 
eststo clear

eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)

drop inst p_N p_N_3

esttab using $out/table3_est3_`type'.tex, /// 
	keep(`inst') /// 
	replace se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(0)) label noobs ///
	nonum nonotes /*noconstant*/ nogaps postfoot("") posthead("\midrule") nolines prehead("{\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}\begin{tabular}{l*{5}{c}|ccccc}\toprule") ///
	mgroups("Panel A: Relative" "Panel B: Total", pattern(1 0 0 0 0 1 0 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	mtitles("Unweighted" "Studies" "Impact" "Precision" "Combined" "Unweighted" "Studies" "Impact" "Precision" "Combined") 

**********
**********

foreach inst in StrictEmpColl_avg AverageTenure_avg CollBarg_avg NetReplacementRate_avg {
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 
eststo clear
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1,  vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_N], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_score], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se], vce(cluster cluster)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study  if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
drop inst p_N p_N_3

esttab using $out/table3_est3_`type'.tex, ///
	keep(`inst') ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01) stats(N, fmt(0)) label noobs ///
	nonum nonotes /*noconstant*/ nogaps postfoot("") prehead("") nolines ///
	mtitles("" "" "" "" "" "" "" "" "" "")
}

**********
**********

local inst index
eststo clear
gen inst=1 if `inst'!=.
bysort paper_id `type' relative inst: gen p_N = 1/_N 
drop inst
gen inst=1 if `inst'!=. & se!=.
bysort paper_id `type' relative inst: gen p_N_3 = 1/_N 

eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1,  vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_N], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_score], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if relative==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1,  vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_N], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_score], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)
eststo: qui reg estimate `inst' $x_ctr $x_macro $x_study if area==1 [pw=p_se*p_score*p_N_3], vce(cluster cluster)
estadd local regionfe "X"
estadd local yearfe "X"
estadd local macro "X"
estadd local study "X"
estadd local skill "X"
estadd local n=round(e(N), .0)

esttab using $out/table3_est3_`type'.tex, ///
	keep(`inst') ///
	scalars("n N" "regionfe \midrule Region FE"  "macro Country-Level Controls" "study Study Characteristics") ///
	append se(3) b(3) star(* 0.10 ** 0.05 *** 0.01)  label noobs ///
	nonum nonotes  nogaps prehead("") nolines posthead("\hline \\") postfoot("\bottomrule \end{tabular}}") ///
	mtitles("" "" "" "" "" "" "" "" "" "")	
	restore
}
